/**
限时折扣
*/
<template>
  <div class="spikeBox sub-main">
    <ActivitySlot
      class="activitySpitze"
      :imgTitle="imgTitle"
    >
      <template #title>
        <div class="title">
          <div class="top">全场{{ discountData.discount || 0 }}折</div>
          <div class="bottom">
            <span
              class="glodTxt"
              v-if="!ifPreheat"
            >距离本场结束：</span>
            <span
              class="glodTxt"
              v-else
            >距离本场开始：</span>
            <div class="timeBox">
              <div class="hour bc">{{ formatHours(time) }}</div>
              <div class="after">:</div>
              <div class="min bc">{{ formatMinutes(time) }}</div>
              <div class="after">:</div>
              <div class="sec bc">{{ formatSeconds(time) }}</div>
            </div>
          </div>
        </div>
      </template>
      <template #section>
        <ul>
          <li></li>
        </ul>
        <div
          class="spitzeList"
          v-if="flag"
        >
          <div
            class="item"
            v-for="item in proList"
            :key="item.productId"
          >
            <ItemSlot
              :data="item"
              @click="toProductDetail(item)"
              @toDetail="toProductDetail(item)"
            >
              <template #extra>
                <div class="progress">
                  <svg-icon
                    style="width: 24px; height: 24px; margin-right: 10px;"
                    name="activity-cart-black"
                  />
                  <span>{{ item.users || item.saleNumber || 0 }} 人已购买</span>
                </div>
              </template>
              <template #icon>
                <svg-icon
                  style="width: 57px;height: 57px;"
                  name="activity-zhekou"
                />
              </template>
              <template #button>
                <button
                  class="buy"
                  :class="ifPreheat ? 'buyWill' : 'butNow'"
                  @click="toProductDetail(item)"
                >{{ ifPreheat ? '未开始' : '立即抢购' }}
                </button>
              </template>
            </ItemSlot>
          </div>
        </div>
        <div
          v-else
          class="noproduct"
        >
          <svg-icon
            style="width: 240px; height: 240px; margin-bottom: 20px;"
            name="user-order-nodata"
          />
          <p class="fs20 font-color-999">该活动没有商品～</p>
        </div>
      </template>
    </ActivitySlot>
    <el-pagination
      style="margin-bottom:20px;"
      background
      layout="prev, pager, next, jumper"
      :page-size="pageSize"
      :current-page="page"
      @current-change="changePage"
      :total="total"
    />
  </div>
</template>

<script setup>
import ActivitySlot from '@/components/activity/activitySlot.vue'
import ItemSlot from '@/views/promotions/components/itemSlot.vue'
import { getDiscountIndex, getDiscountList, getPlatformDiscountList } from '@/api/Activity/ActivityDiscount.js'
import { defineComponent, onMounted, onUnmounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useLoading } from '@/hooks/useLoading'
import SvgIcon from '@/components/svgIcon/index.vue';

defineComponent({
  name:'Spitze'
})
const { showLoading,closeLoading } = useLoading()

function formatHours (value) {
  if (!value) {
    return '00'
  }
  let hours = Math.floor(value / (1000 * 60 * 60))
  if (hours < 10) {
    return '0' + hours
  } else {
    return hours
  }
}

function formatMinutes (value) {
  if (!value) {
    return '00'
  }
  let minutes = Math.floor((value % (1000 * 60 * 60)) / (1000 * 60))
  if (minutes < 10) {
    return '0' + minutes
  } else {
    return minutes
  }
}

function formatSeconds (value) {
  if (!value) {
    return '00'
  }
  let seconds = Math.round((value % (1000 * 60)) / 1000)
  if (seconds < 10) {
    return '0' + seconds
  } else {
    return seconds
  }
}

const imgTitle = ref('static/image/activity/spitzeTitle.webp')
const page = ref(1)
const pageSize = ref(12)
const total = ref(0)
const id = ref('')
const shopId = ref('')
const proList = ref([])
const time = ref('')
const setTime = ref(null)
const ifPreheat = ref(false)
const type = ref('')
const volume = ref('')
const index = ref('')
const flag = ref(true)
const discountData = ref({})

const route = useRoute()
const router = useRouter()
onMounted(()=>{
  if (route.query.id) {
    id.value = route.query.id
  }
  if (route.query.shopId) {
    shopId.value = route.query.shopId
    getDiscount()
  } else {
    getDiscounts()
    getPlatformDiscount()
  }
})
onUnmounted(()=>{
  clearInterval(setTime.value)
})
// 倒计时
function countDown () {
  clearInterval(setTime.value)
  setTime.value = setInterval(() => {
    if (time.value < 1000) {
      clearInterval(setTime.value)
      return alert('活动已结束')
    }
    time.value -= 1000
  }, 1000)
}
// 跳转到商品详情
function toProductDetail (item) {
  let data = {
    productId: item.productId,
    shopId: item.shopId,
    skuId: item.skuId
  }
  router.push({
    path: '/productDetail',
    query: {
      proData: JSON.stringify(data)
    }
  })
}
// 获取平台限时折扣
async function getPlatformDiscount () {
  showLoading()
  const response = await getPlatformDiscountList({
    page: page.value,
    pageSize: pageSize.value,
    discountId: route.query.id,
    type: '',
    volume: ''
  })
  const res = response.data
  if (res.code === '200') {
    proList.value = res.data.list
    total.value = parseInt(res.data.total)
    time.value = new Date(discountData.value.endTime).getTime() - new Date().getTime()
    if (res.data.list.length) {
      flag.value = true
    } else {
      flag.value = false
      time.value = 0
    }
    if (id.value !== '' && flag.value) {
      countDown()
    }
  }
  closeLoading()
}

async function getDiscounts () {
  const response = await getDiscountList({ discountId: route.query.id })
  const res = response.data
  if (res.code === '200') {
    discountData.value = res.data
  } else {
    ElMessage.error(res.message)
  }
}

// 获取店铺首页
async function getDiscount () {
  showLoading()
  let params = {
    shopId: shopId.value,
    shopDiscountId: id.value,
    page: page.value,
    pageSize: pageSize.value
  }
  if (id.value === '') {
    params.volume = volume.value
    params.type = type.value
  }
  const response = await getDiscountIndex(params)
  const res = response.data
  if (res.code === '200') {
    ifPreheat.value = res.data.state !== 1
    proList.value = res.data.page.list
    total.value = res.data.page.total
    time.value = ifPreheat.value ? res.data.enableTime : res.data.time
    if (res.data.page.list.length) {
      flag.value = true
    } else {
      flag.value = false
      time.value = 0
    }
    if (id.value !== '' && flag.value) {
      countDown()
    }
    closeLoading()
  } else {
    ElMessage.warning(res.message)
  }
}

// 翻页
function changePage (val) {
  page.value = val
  getDiscount()
}

// 默认
function defaultSort () {
  index.value = '1'
  page.value = 1
  type.value = ''
  volume.value = ''
  getDiscount()
}

// 价格
function priceSort () {
  index.value = '2'
  page.value= 1
  if (type.value === '' || type.value === 2) {
    type.value.valuee = 1
  } else {
    type.value = 2
  }
  volume.value = ''
  getDiscount()
}
// 销量
function salesSort () {
  index.value = '3'
  page.value = 1
  if (volume.value === '' || volume.value === 2) {
    volume.value = 1
  } else {
    volume.value = 2
  }
  type.value = ''
  getDiscount()
}
</script>

<style
  lang="scss"
  scoped
>
$zhekouHeight: 40px;
$zhekouTitleFS: 16px;
.spikeBox {
  max-width: 1250px;
  margin: auto;

  .activitySpitze {
    position: relative;

    .title {
      width: 40%;
      height: 100px;
      color: #FFF;
      margin: auto;
      text-align: center;
      position: absolute;
      top: 130px;
      left: 0;
      right: 0;

      .top {
        width: 146px;
        height: 60px;
        margin: auto;
        text-align: center;
        color: #C83732;
        font-size: 32px;
        font-family: Microsoft YaHei;
      }

      .bottom {
        display: flex;
        justify-content: center;
        align-items: center;

        .glodTxt {
          color: #FFEBC4;
          font-size: $zhekouTitleFS;
          font-family: Microsoft YaHei;
        }

        .timeBox {
          height: $zhekouHeight;
          display: flex;
          align-items: center;

          .after {
            font-size: $zhekouTitleFS;
            margin: 0 13px;
          }

          .bc {
            width: $zhekouHeight;
            height: $zhekouHeight;
            line-height: $zhekouHeight;
            font-size: $zhekouTitleFS;
            font-family: Microsoft YaHei;
            font-weight: bold;
            color: #FFFFFF;
            background-color: #333333;
          }
        }
      }
    }

    .spitzeList {
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;

      .item {
        width: 23%;
        margin: 20px 1%;
        transition: all 0.3s linear;

        .progress {
          width: 100%;
          height: 40px;
          background-color: #DADBDC;
          position: absolute;
          bottom: 0;
          display: flex;
          justify-content: center;
          align-items: center;

          span {
            font-size: 14px;
            color: #333333;
          }
        }

        .buy {
          width: 100%;
          height: 40px;
          padding: 0;
          margin: 15px 0 0;
          color: #FFF;
          background: linear-gradient(90deg, #C83732 0%, #E25C44 100%);
          box-shadow: 0px 3px 6px rgba(233, 0, 0, 0.3);
          cursor: pointer;
        }

        .butNow {
          background: linear-gradient(90deg, #C83732 0%, #E25C44 100%);
          box-shadow: 0px 3px 6px rgba(233, 0, 0, 0.3);
        }

        .buyWill {
          background: linear-gradient(90deg, #29C790 0%, #75D98C 100%);
          box-shadow: 0px 3px 6px rgba(52, 203, 144, 0.3);
        }
      }

      .item:hover {
        box-shadow: 0 0 20px #cccccc;
      }
    }
  }

  .spikeTit {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 60px;

    h3 {
      font-size: 36px;
      color: #333333;
      font-weight: bold;
    }

    .countdown {
      display: flex;
      align-items: center;
      margin-left: 50px;

      .countdownTit {
        font-size: 24px;
        color: #333333;
        font-weight: bold;
      }

      .timeDown {
        margin-left: 10px;

        span {
          display: inline-block;
          text-align: center;
          font-size: 24px;
          height: 40px;
          line-height: 40px;
        }

        .time {
          min-width: 40px;
          background: #000;
          color: #FFFFFF;
        }
      }
    }
  }

  .sort {
    width: 100%;
    height: 63px;
    line-height: 27px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #f1f1f1;
    color: #666666;
    margin-bottom: 28px;

    .default {
      margin-left: 20px;
      cursor: pointer;
    }

    .sales {
      margin-right: 20px;
      cursor: pointer;

      img {
        width: 9px;
        height: 16px;
        vertical-align: middle;
      }
    }

    .price {
      display: flex;
      margin: 0 20px;
      cursor: pointer;

      .arr {
        vertical-align: middle;
        width: 12px;
        margin-left: 3px;

        .up {
          width: 0;
          height: 0;
          border: 6px solid transparent;
          border-bottom-color: #999999;
        }

        .down {
          width: 0;
          height: 0;
          border: 6px solid transparent;
          border-top-color: #999999;
          margin-top: 3px;
        }
      }
    }

    .active {
      color: #FF7800;

      .arr {
        .high {
          border-top-color: #FF7800;
        }

        .low {
          border-bottom-color: #FF7800;
        }
      }
    }
  }

  .spikeList {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;

    .item {
      width: 282px;
      margin: 0 40px 65px 0;
      transition: all 0.3s linear;

      .imgBox {
        height: 282px;
        margin-bottom: 25px;
        cursor: pointer;

        img {
          width: 100%;
          height: 100%;
        }
      }

      &:nth-child(4n) {
        margin-right: 0;
      }

      .priceBox {
        text-align: center;

        h3 {
          font-size: 16px;
          font-weight: bold;
          color: #333434;
          margin-bottom: 12px;
          cursor: pointer;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }

        .price {
          width: 200px;
          font-size: 18px;
          font-weight: bold;
          color: $mainColor;
          display: block;
          padding-bottom: 10px;
          border-bottom: 2px solid #f3f3f3;
          margin: 0 auto 13px auto;
        }

        .people {
          font-size: 14px;
          font-weight: 400;
          color: #999999;
          display: block;
          margin-bottom: 16px;
        }

        button {
          width: 120px;
          height: 34px;
          background: linear-gradient(247deg, #F71622, #FE6F52);
          border: 1px solid #EC4B42;
          border-radius: 4px;
          color: #FFFFFF;
          cursor: pointer;
        }
      }
    }

    .item:hover {
      box-shadow: 0 0 20px #cccccc;
    }
  }

  .noproduct {
    width: 100%;
    text-align: center;
    min-height: 500px;

    img {
      margin-top: 50px;
    }
  }
}
</style>
